#!/usr/bin/env bash

######################################################################
the_profile="$HOME/.profile"

inp_mkfile()
{
   if [ ! -f $the_profile ]; then
       touch $the_profile
       echo "-- $the_profile is noe exsit, create it by install_dependence.sh"
   fi
}

insert_profile_into_bash()
{
    if [ "`uname`"x == "Darwin"x ] ; then
        grep "/.profile" $HOME/.bash_profile > /dev/null
        if [ $? -ne 0 ]; then
            printf "\tif [ -f ~/.profile ]; then\n\t\t. ~/.profile\n\tfi\n" >> $HOME/.bash_profile
        fi
    fi
}

inp_calculate_exist_flag()
{
    local the_value=$1
    local real_value=$2
    value_exist_flag=$(echo $the_value | awk -v value="$real_value" -F ":" '   {
    for(i=1;i<=NF;++i)
        {
            if($i == value)                    
                {
                    printf("exist");
                    exit;
                }
        }
    printf("not_exist");
}')
}

inp_new_key_value()
{
    local key=$1
    local value=$2
    if [ -n "$(eval echo \$$key)" ] ; then
        printf "export %s=\$%s:%s\n" "$key" "$key" "$value" >> $the_profile
        echo "-- Info: [$key] does not exist in file $the_profile, and $key not empty, it can repeat,add it"
    else
        printf "export %s=%s\n" "$key" "$value" >> $the_profile
        echo "-- Info: [$key] does not exist in file $the_profile,it can repeat,add it"
    fi
}

if [ "$(uname)" == "Darwin" ] ; then
    cmd_sed="sed -i '' "
else
    cmd_sed="sed -i "
fi

inp_append_key_value()
{
    local key=$1
    local value=$2

    echo grep -E -v "^[ *#+]" $the_profile | grep -n -w "$key"  >/dev/null
    #grep -E -v "^[ *#+]" $the_profile | grep -n -w "$key"  >/dev/null
    #if [ $? -ne 0 ]; then
    #    inp_new_key_value $key $value
    #else  #already exist in the profile, append new data after line
        echo "-- Info: [$key] already exist in file $the_profile, because it is multiple value,append $value"
        key_line=`grep -n -E -v "^[ #]+" $the_profile | grep -w "$key" | cut -d ':' -f 1 | tail -n 1`
        $cmd_sed "${key_line},${key_line}s%$%:$value%" $the_profile
    #fi

}

inp_prepend_key_value()
{
    local key=$1
    local value=$2

    #grep -E -v "^[ *#+]" $the_profile | grep -n -w "$key"  >/dev/null
    #if [ $? -ne 0 ]; then
    #    inp_new_key_value $key $value
    #else  #already exist in the profile, append new data after line
        echo "-- Info: [$key] already exist in file $the_profile, because it is multiple value,prepend $value"
        key_line=`grep -n -E -v "^[ #]+" $the_profile | grep -w "$key" | cut -d ':' -f 1 | tail -n 1`
	#cat <<EOF
    #    $cmd_sed "${key_line},${key_line}s#$key=#&${value}:#" $the_profile
#EOF
        $cmd_sed "${key_line},${key_line}s#$key=#&${value}:#" $the_profile
        #$cmd_sed "${key_line},${key_line}s/$key=/&${value//\//\\\/}:/" $the_profile
    #fi
}

#param:
#    $1:key
#    $2:env value
#    $3:1,is unique.  0,can set multiple times
#    $4:1,is append.  0,prepend  (need $3=0)
insert_profile()
{
    inp_mkfile
    source $the_profile
    insert_profile_into_bash

    local the_env="$1=$2"
    #key=`echo $the_env | cut -d '=' -f 1`
    #value=`echo $the_env | cut -d '=' -f 2`
    local key=$1
    local value=$2
    if [ $3 -eq 1 ]; then  #the key can only defined once, key=value is single
        grep -E -v "^[ *#+]" $the_profile | grep -n -w "$key"  >/dev/null
        if [ $? -ne 0 ]; then
            printf "export $the_env\n" >> $the_profile
            echo "-- Info: [$the_env] does not exist in file $the_profile,add $the_env"
        else  #already exist,delete older and inster newer
            echo "-- Info: [$the_env] already exist in file $the_profile.because it is unique,changing the value to $value"
            key_line=`grep -n -w "$key" $the_profile | cut -d ':' -f 1 | tail -n 1`
            $cmd_sed "${key_line}a\export $the_env" $the_profile
            $cmd_sed "${key_line}d" $the_profile
        fi
    elif [ $3 -eq 0 ]; then  #the env can repeat, is not unique, key=v1:v2 value is multiple
        if [ "$key"x == "PATH"x ] || [ "$key"x == "LD_LIBRARY_PATH"x ] ; then
            if ! grep -w $key $the_profile > /dev/null ; then
                echo "export $key=\$$key" >> $the_profile
            fi
        fi

        the_value=`eval echo -n '$'$key`
        #is empty, there is not the key in the profile
        if [ -z "$the_value" ] ; then
            echo "-- Info: [$key] is not exist, insert new key value, set $key=$value"
            printf "export %s=%s\n" "$key" "$value" >> $the_profile
        else #not empty
            real_value=`eval echo $value`
            inp_calculate_exist_flag $the_value $real_value
            if [ "$value_exist_flag" == "exist" ] ; then
                echo "-- Info: [$value] already in \$$key, checking by: \"env | grep -w $key\", now doing nothing"
            elif [ "$value_exist_flag" == "not_exist" ]; then
                if [ -z "$4" ] || [ "$4"x == "1"x ] ; then #append
                    inp_append_key_value $key $value
                elif [ -n "$4" ] || [ "$4"x == "0"x ] ; then #prepend
                    inp_prepend_key_value $key $value
                fi
            fi
            unset value_exist_flag
        fi
    fi #end if $3
    source $the_profile
}



      